__author__ = 'Muscle'
from mui import *
from mui.Widgets import *
import time
import mui.color as color


class MyDialog(MUIDialog):
    def __init__(self, parent=None, **argd):
        argd.setdefault('width', 400)
        argd.setdefault('height', 300)
        argd.setdefault('bgColor', color.blue)
        super(MyDialog, self).__init__(parent, **argd)
        self.caption = MUICaption(self, width = 400, height = 30, bgColor = color.green, caption = 'caption Test', fontColor = color.black)

        self.btn_ok = MUITextButton(self, left = 20, top = 20, width = 100, height = 40, text = 'ok', command = 'ok')
        self.btn_cancel = MUITextButton(self, left = 150, top = 20, width = 100, height = 40, text = 'cancel', command = 'cancel')
        self.bindCmd('ok', self.onOk, noarg = True)
        self.bindCmd('cancel', self.onCancel, noarg = True)
        self.dock(self.btn_cancel, { 'b':50, 'r':50})
        self.dock(self.btn_ok, { 'b':50, 'r':200})
        self.dock(self.caption, { 't':0, 'l':0, 'r':0})

class MyDialogWindow(MUIDialogWindow):
    def __init__(self, **argd):
        argd.setdefault('width', 400)
        argd.setdefault('height', 300)
        argd.setdefault('bgColor', color.blue)
        super(MyDialogWindow, self).__init__( **argd)

        self.btn_ok = MUITextButton(self, left = 20, top = 20, width = 100, height = 40, text = 'ok', command = 'ok')
        self.btn_cancel = MUITextButton(self, left = 150, top = 20, width = 100, height = 40, text = 'cancel', command = 'cancel')
        self.bindCmd('ok', self.onOk, noarg = True)
        self.bindCmd('cancel', self.onCancel, noarg = True)

    def on_paint(self):
        super(MyDialogWindow, self).on_paint()

class MyWindow(MUIWindow):
    def __init__(self, **kwargs):
        MUIWindow.__init__(self, **kwargs)
        self.background = 'dialog\\volume_bg.png |(10,10,10,10)'
        self.button1 = MUITextButton(self, left = 20, top = 20, width = 200, height = 40, name = 'btn', text = 'test Dialog', fontColor = color.white, command = 'test Dialog')
        self.button2 = MUITextButton(self, left = 20, top = 250, width = 200, height = 40, name = 'btn', text = 'test DialogWindow', fontColor = color.white, command = 'test DialogWindow')

        self.bindCmd('test Dialog', self.onTestDialog)
        self.bindCmd('test DialogWindow', self.onTestDialogWindow)
        # for FPS
        self.__fpsCount = 0
        self.__fpsTimeStart = 0
        self.__fosTimeEnd = 0
        self.fpsText = MUIText(self, right = self.width-20, top = 20, width = 200, height = 40, text = "FPS: 0", fontSize = 20, fontColor = color.blue, fontWeight = 'Bold', blind = True)

        self.bindData('width', self.fpsText, 'right', dir = '->', converter =  lambda x: x-20 )

    def on_paint(self):
        if (self.__fpsCount == 0):
            self.__fpsTimeStart = time.clock()
        super(MyWindow, self).on_paint()
        self.__fpsCount += 1

        if (self.__fpsCount >= 120):
            self.__fpsTimeEnd = time.clock()
            diffTime = self.__fpsTimeEnd - self.__fpsTimeStart
            fps = self.__fpsCount / diffTime
            self.fpsText.text = "FPS: " + str(int(fps))
            self.__fpsCount = 0

    def onTestDialog(self, comp):
        dlg = MyDialog(self, left = 50, top = 50)
        dlg.doModal()

    def onTestDialogWindow(self, comp):
        sx, sy = self.local2screen(0, 0)
        dlg = MyDialogWindow(owner = self, winPosX = sx, winPosY = sy)
        dlg.ShowWindow(True)
        dlg.doModal()
        
def main():
    mui.muiInit(main)
    g_DXDevice = muiDX.CDXDevice()
    w = MyWindow(width = 1024, height = 768, dxDevice = g_DXDevice)
    w.ShowWindow(1)
    muiApp.run()
    g_DXDevice.Release()
    mui.muiUnInit()



main()